<template>
    <div class="warp-2 body-content" style="height: 100%;">
        <navbar :title="$t('message_detail')"></navbar>
        <div class="content-noBottom">
            <div class="box-tab-01 paddingLR15 white-bg bottom-shadow-02">
                <ul class="clearfix">
                    <li class="color666" v-bind:class="{'cur' : index == 0}" @click="selectType(0)">{{ $t( 'message_not_read' ) + (notReadCount == 0 ? '' : '(' + notReadCount + ')') }}</li><!--收件箱-->
                    <li class="color666" v-bind:class="{'cur' : index == 1}" @click="selectType(1)">{{ $t( 'message_read' ) }}</li><!--发件箱-->
                </ul>
            </div>
            <div style="height: calc(100% - 44px);" :class="{'height-100' : total === 0}">
                <van-pull-refresh v-model="isRefresh" @refresh="onPulldownTop" loading-text="loading" pulling-text="" loosing-text="" style="height: 100%;">
                    <van-list
                    v-model="isLoading"
                    :finished="finished"
                    :finished-text="(finished&&total>0) ? $t('no_more') : ''"
                    loding-text="loading"
                    @load="getMessageList"
                    >
                        <div v-bind:class="{'height100' : total === 0}">
                            <ul class="module-list-19">
                                <!--收件箱-->
                                <li class="clearfix" v-for="message in messageList" :key="message.wmsgId">
                                    <div @click="showDetail(message)">
                                        <div>{{ message.wmsgSubject }}</div>
                                        <div style="text-align: right">{{ message.wmsgCreateTimestamp }}</div>
                                    </div>
                                </li>
                            </ul>
                            <!--暂无数据-->
                            <div v-if="!isLoading && total === 0" style="height:100%;">
                                <div class="box-table" style="width:100%;height:100%;">
                                    <div class="box-table-cell" style="text-align: center;">
                                        <div class="no-data">
                                            <img src="../../../static/images/no-data.png" alt=""/>
                                            <p class="color999">{{ $t("no_data") }}</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </van-list>
                </van-pull-refresh>
            </div>
        </div>
        <common-loading :isLoading="isLoading"></common-loading>
    </div>
</template>

<script>
export default {
    name: 'NewMessage',
    data() {
        return {
            pageNo: 1,
            index: 0,
            messageList: [],
            total: 0,
            deleteInfo: {},
            currUserEntId: window.localStorage.getItem('loginUser'),
            isLoading: true,
            notReadCount: 0,
            finished: false,
            isRefresh: false
        };
    },
    methods: {
        showDetail(message) {
            let msgId = message.wmsgId;
            this.$router.push({path: `/messageDetail/${msgId}/${this.index}`});
        },
        /*下拉刷新*/
        onPulldownTop() {
            this.isRefresh = true;
            setTimeout(() => {
                this.finished = false;
                this.pageNo = 1;
                this.getMessageList('refresh');
                this.isRefresh = false;
            }, 900);
        },
        getMessageList(option) {
            this.$axios({
                method: 'POST',
                url: `/app/api/trainee/my/message/web-message/list`,
                params: {
                    sortname: 'wmsg_create_timestamp',
                    sortorder: 'desc',
                    pageNo: this.pageNo
                },
                data: {
                    wmrhStatus: this.index === 0 ? 'N' : 'Y'
                }
            }).then(res => {
                if (res.data.results && res.data.results.length !== 0) {
                    if (option && option === 'refresh') {
                        this.messageList = res.data.results;
                    } else {
                        this.messageList = [...this.messageList, ...res.data.results];
                    }
                }
                if (this.index === 0) {
                    this.notReadCount = res.data.totalRecord;
                }
                this.total = res.data.totalRecord;
                this.isLoading = false;
                this.pageNo++;
                if (this.messageList.length >= this.total) {
                    this.finished = true;
                }
            });
        },
        selectType(index) {
            this.index = index;
            this.pageNo = 1;
            this.messageList = [];
            this.onFetching = false;
            this.isLoading = true;
            this.getMessageList();
        }
    },
    created() {
        this.getMessageList();
    }
};
</script>

<style scoped lang="less">
    .module-list-19 {
        li {
            padding: 20px 15px;
            border-bottom: 1px solid #e6e6e6;
            background: #ffffff;
            position: relative;
            .mess-icon {
                float: left;
                margin-left: -69px;
                width: 50px;
                height: 50px;
                border-radius: 50%;
                background: #eeeeee;
                position: relative;
                .current {
                    width: 6px;
                    height: 6px;
                    display: inline-block;
                    background: #fd3b31;
                    border: 2px solid #fafafa;
                    border-radius: 50%;
                    position: absolute;
                    top: 2px;
                    right: 3px;
                }
                &:before {
                    content: "";
                    width: 20px;
                    height: 20px;
                    display: block;
                    background: url(./images/mess-icon.png) no-repeat 2px 0;
                    background-size: 15px;
                    margin: 15px auto 0;
                }
                &.cur:before {
                    content: "";
                    background-position: 2px -21px;
                }
            }
            .title-box {
                float: left;
                width: 100%;
                .time-box {
                    float: left;
                    margin-top: 15px;
                }
            }
        }
    }
    .delete-icon {
        position: absolute;
        right: 15px;
        bottom: 23px;
        padding: 15px 0 0 15px;
        height: 16px;
        z-index: 2;
        &:before {
            content: "";
            margin-top: -3px;
        }
    }
    /*发站内信图标*/
    .add-icon-box {
        bottom: 19px;
    }
</style>
